﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Traxz.Models;
using System.Web.Security;

namespace Traxz.Controllers
{
    public class ExerciseSessionController : Controller
    {

        TraxzRepository repo = new TraxzRepository();

        public ActionResult Index()
        {

            return View(repo.getListSessions());
        }

        //page is the current page of the list we are on so in our list page*10 is the index we start on
        [Authorize]
        public ActionResult listSessions(int? page, Guid? SessionID)
        {
            int nPage = 0;
            if (page != null)
            {
                nPage = ((int)page);
            }

            Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
            List<ExerciseSession> listToPass = new List<ExerciseSession>();
            List<ExerciseSession> exList = repo.getListOfUserExerciseSessions(userGuid);
            for(int i = nPage; i < exList.Count; i++)
            {
                listToPass.Add(exList[i]);
                nPage++;
            }
            //on initial entering of the view session
            if (SessionID.Equals(null))
            {
                LatLon[] latlonARR = exList[0].LatLons.ToArray();
                ViewBag.LatLons = latlonARR;
                ViewBag.CurrSess = exList[0];
                ViewBag.LatLonSize = latlonARR.Length;

            }
                //otherwise user has selected a session from side list--Query and put in viewbag
            else
            {
                var currSess = repo.getSession((Guid)SessionID);
                ViewBag.LatLons = currSess.LatLons;
                ViewBag.CurrSess = currSess;
            }

            nPage = nPage - 1;
            if (exList.Count - nPage < 1)
            {
                ViewBag.pageNum = nPage + 1;
            }
            else
            {
                ViewBag.pageNum = nPage;
            }
            ViewBag.ESList = listToPass;
            return View();
        }

        private List<float> getLatLonList(List<LatLon> latlons)
        {
            List<float> toRet = new List<float>();
            foreach (var i in latlons)
            {
                toRet.Add((float)i.Latitude);
                toRet.Add((float)i.Longitude);
            }
            return toRet;
        }

    }
}
